const Mock = require('mockjs');

const apiPrefix = '/users';

let usersListData = Mock.mock({
    'data|80-120': [
        {
            id: '@id',
            name: '@name',
            nickName: '@last',
            phone: /^1[34578]\d{9}$/,
            'age|11-99': 1,
            address: '@county(true)',
            email: '@email',
            createTime: '@datetime',
        },
    ],
});


let database = usersListData.data;

module.exports = {

    [`POST ${apiPrefix}/list`] (req, res) {
        const params = req.body;
        let { pageSize, page, ...other } = params;
        pageSize = pageSize || 10;
        page = page || 1;

        let newData = database;
        for (let key in other) {
            if ({}.hasOwnProperty.call(other, key)) {
                newData = newData.filter((item) => {
                    if ({}.hasOwnProperty.call(item, key)) {
                        if (key === 'address') {
                            return other[key].every(iitem => item[key].indexOf(iitem) > -1)
                        } else if (key === 'createTime') {
                            const start = new Date(other[key][0]).getTime();
                            const end = new Date(other[key][1]).getTime();
                            const now = new Date(item[key]).getTime();

                            if (start && end) {
                                return now >= start && now <= end
                            }
                            return true
                        } else if (key === 'phone'){
                            return other[key] === item[key]
                        }
                        return String(item[key]).trim().indexOf(decodeURI(other[key]).trim()) > -1
                    }
                    return true
                })
            }
        }

        res.status(200).json({
            success: true,
            data: newData.slice((page - 1) * pageSize, page * pageSize),
            total: newData.length,
            page: parseInt(page,10),
            pageSize: parseInt(pageSize,10)
        })
    },

    [`POST ${apiPrefix}/delete`] (req, res) {
        const { id } = req.body;
        database = database.filter(item => item.id !== id);
        res.status(200).json({
            success: true,
            message: '删除成功'
        })
    },

    [`POST ${apiPrefix}/update`] (req, res) {
        const editItem = req.body;
        let isExist = false;
        database = database.map((item) => {
            if (item.id === editItem.id) {
                isExist = true;
                return Object.assign({}, item, editItem)
            }
            return item
        });
        if (isExist) {
            res.status(200).json({
                success: true,
                message: '更新成功'
            })
        } else {
            res.status(200).json({
                success: false,
                message: '更新失败',
                data: editItem
            })
        }
    },

    [`POST ${apiPrefix}/add`] (req, res) {
        const newData = req.body;
        newData.createTime = Mock.mock('@now');
        newData.id = Mock.mock('@id');
        database.unshift(newData);
        res.status(201).json({
            success: true,
            message: '新增成功'
        })
    },

    [`POST ${apiPrefix}/get`] (req, res) {
        const { id } = req.body;
        let newData = database.filter(item => item.id === id);
        res.status(201).json({
            success: true,
            message: '成功',
            data: newData[0]
        })
    }
};
